package com.shareware.common.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.PixelFormat
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.view.View
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream

/**
 * desc: 功能描述
 * email: mobiledeveloper@qq.com
 */

/**
 * Bitmap--byte[]
 * @return byte[]
 */

fun Bitmap.toBytes(): ByteArray {
    return ByteArrayOutputStream().also {
        this.compress(Bitmap.CompressFormat.PNG, 100, it)
    }.toByteArray()
}

/**
 * byte[]--Bitmap
 * @return Bitmap
 */
fun ByteArray.toBitmap(): Bitmap {
    return BitmapFactory.decodeByteArray(this, 0, this.size)
}


/**
 * Drawable -- bitmap
 *
 * @return bitmap
 */

fun Drawable.toBitmap(): Bitmap {
    //获取drawable的长和宽
    val width = this.intrinsicWidth
    val height = this.intrinsicHeight
    //取drawable得颜色格式
    val config = if (this.opacity == PixelFormat.OPAQUE)
        Bitmap.Config.RGB_565 else Bitmap.Config.ARGB_8888
    //建立对应的bitmap
    val bitmap = Bitmap.createBitmap(width, height, config)
    //建立对应bitmap的画布
    val canvas = Canvas(bitmap)
    this.setBounds(0, 0, width, height)
    //把drawable内容画到画布上
    this.draw(canvas)
    return bitmap
}

/**
 * bitmap--drawable
 *
 * @param context context
 * @return drawable
 */

fun Bitmap.toDrawable(context: Context): Drawable {
    return BitmapDrawable(context.resources, this)
}

/**
 * 放大或缩小图片
 *
 * @param newWidth  newWidth
 * @param newHeight newHeight
 * @return Bitmap
 */
fun Bitmap.zoomBitmap(newWidth: Int, newHeight: Int): Bitmap {
    // 获得图片的宽高
    val width = this.width
    val height = this.height
    // 计算缩放比例
    val scaleWidth = newWidth.toFloat() / width
    val scaleHeight = newHeight.toFloat() / height
    val scale = if (scaleWidth > scaleHeight) scaleHeight else scaleWidth
    // 取得想要缩放的matrix参数
    val matrix = Matrix()
    matrix.postScale(scale, scale)
    // 得到新的图片
    return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
}

/**
 * 处理图片绘制位置，在width和height中显示居中
 *
 * @param width  width
 * @param height height
 * @param matrix matrix
 */
fun Bitmap.centerIn(width: Int, height: Int, matrix: Matrix) {
    val left = ((width - this.width) / 2).toFloat()
    val top = ((height - this.height) / 2).toFloat()
    //缩放后
    if (this.width < width && this.height < height) {
        matrix.setTranslate(left, top)
    } else if (this.width < width) {
        matrix.setTranslate(left, 0f)
    } else if (this.height < height) {
        matrix.setTranslate(0f, top)
    }
}

/**
 * 获取一个 View 的缓存视图
 * @return Bitmap
 */

fun View.getCacheBitmap(): Bitmap {
    val drawingCacheEnabled = isDrawingCacheEnabled
    val willNotCacheDrawing = willNotCacheDrawing()
    isDrawingCacheEnabled = true
    setWillNotCacheDrawing(false)
    var drawingCache = this.drawingCache
    val bitmap: Bitmap
    if (null == drawingCache || drawingCache.isRecycled) {
        measure(
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        )
        layout(0, 0, measuredWidth, measuredHeight)
        buildDrawingCache()
        drawingCache = this.drawingCache
        if (null == drawingCache || drawingCache.isRecycled) {
            bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.RGB_565)
            val canvas = Canvas(bitmap)
            draw(canvas)
        } else {
            bitmap = Bitmap.createBitmap(drawingCache)
        }
    } else {
        bitmap = Bitmap.createBitmap(drawingCache)
    }
    setWillNotCacheDrawing(willNotCacheDrawing)
    isDrawingCacheEnabled = drawingCacheEnabled
    destroyDrawingCache()
    return bitmap
}

/**
 * @param file 保存的文件
 */
fun Bitmap.saveImage(file: File) {
    FileOutputStream(file).use {
        this.compress(Bitmap.CompressFormat.PNG, 100, it)
        it.flush()
    }
}